package org.lql.algo.codecrush.week009.question.type.II;

import org.lql.algo.common.TreeNode;

/**
 * @author: liangqinglong
 * @date: 2025-08-20 16:56
 * @description: 513. 找树左下角的值 <a href="https://leetcode.cn/problems/find-bottom-left-tree-value/description/">...</a>
 **/
public class FindBottomLeftValue {

	public int curMaxDepth = 0;
	public int curVal = 0;

	public int findBottomLeftValue(TreeNode root) {
		int currDepth = 0;
		dfs(root, currDepth);
		return curVal;
	}

	private void dfs(TreeNode root, int currDepth) {
		if (root == null) {
			return;
		}
		currDepth++;
		dfs(root.left, currDepth);
		dfs(root.right, currDepth);
		if (currDepth > curMaxDepth) {
			curMaxDepth = currDepth;
			curVal = root.val;
		}
	}

	public static void main(String[] args) {
		FindBottomLeftValue f = new FindBottomLeftValue();
		TreeNode root = new TreeNode(1);
		root.left = new TreeNode(2);
		root.right = new TreeNode(3);
		root.left.left = new TreeNode(4);
		root.right.left = new TreeNode(5);
		root.right.right = new TreeNode(6);
		root.right.left.left = new TreeNode(7);
		System.out.println(f.findBottomLeftValue(root));
	}
}
